﻿using AyuBookmark.Common.Models;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace AyuBookmark.WinForm.Dialogs
{
    /// <summary>
    /// 书签编辑对话框
    /// </summary>
    public partial class BookmarkEditor : Form
    {
        // UI控件声明
        private TextBox txtTitle;
        private TextBox txtUrl;
        private ComboBox cmbCategory;
        private TextBox txtDescription;
        private Button btnSave;
        private Button btnCancel;
        private Button btnOpenUrl;
        private Label lblTitle;
        private Label lblUrl;
        private Label lblCategory;
        private Label lblDescription;
        private Panel panelButtons;

        /// <summary>
        /// 获取或设置当前编辑的书签
        /// </summary>
        public Bookmark CurrentBookmark { get; set; }

        public BookmarkEditor(Bookmark bookmark = null)
        {
            InitializeComponent();
            CurrentBookmark = bookmark ?? new Bookmark();
            InitializeUI();
            LoadBookmarkData();
        }

        private void InitializeComponent()
        {
            // 窗体基础设置
            this.SuspendLayout();
            this.Text = CurrentBookmark.Id == 0 ? "添加书签" : "编辑书签";
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ClientSize = new Size(500, 300);
            this.BackColor = SystemColors.Window;
            this.Padding = new Padding(10);

            // 标题标签
            this.lblTitle = new Label();
            this.lblTitle.Text = "标题:";
            this.lblTitle.AutoSize = true;
            this.lblTitle.Location = new Point(20, 20);
            this.lblTitle.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);

            // 标题文本框
            this.txtTitle = new TextBox();
            this.txtTitle.Location = new Point(100, 17);
            this.txtTitle.Size = new Size(370, 25);
            this.txtTitle.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            this.txtTitle.Font = new Font("Microsoft YaHei", 9f);

            // URL标签
            this.lblUrl = new Label();
            this.lblUrl.Text = "URL:";
            this.lblUrl.AutoSize = true;
            this.lblUrl.Location = new Point(20, 60);
            this.lblUrl.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);

            // URL文本框
            this.txtUrl = new TextBox();
            this.txtUrl.Location = new Point(100, 57);
            this.txtUrl.Size = new Size(320, 25);
            this.txtUrl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            this.txtUrl.Font = new Font("Microsoft YaHei", 9f);

            // 打开URL按钮
            this.btnOpenUrl = new Button();
            this.btnOpenUrl.Text = "打开";
            this.btnOpenUrl.Size = new Size(50, 25);
            this.btnOpenUrl.Location = new Point(425, 57);
            this.btnOpenUrl.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            this.btnOpenUrl.Font = new Font("Microsoft YaHei", 8f);
            this.btnOpenUrl.Click += (s, e) => OpenUrl();

            // 分类标签
            this.lblCategory = new Label();
            this.lblCategory.Text = "分类:";
            this.lblCategory.AutoSize = true;
            this.lblCategory.Location = new Point(20, 100);
            this.lblCategory.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);

            // 分类下拉框
            this.cmbCategory = new ComboBox();
            this.cmbCategory.Location = new Point(100, 97);
            this.cmbCategory.Size = new Size(370, 25);
            this.cmbCategory.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            this.cmbCategory.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbCategory.Font = new Font("Microsoft YaHei", 9f);

            // 描述标签
            this.lblDescription = new Label();
            this.lblDescription.Text = "描述:";
            this.lblDescription.AutoSize = true;
            this.lblDescription.Location = new Point(20, 140);
            this.lblDescription.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);

            // 描述文本框
            this.txtDescription = new TextBox();
            this.txtDescription.Location = new Point(100, 137);
            this.txtDescription.Size = new Size(370, 80);
            this.txtDescription.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
            this.txtDescription.Multiline = true;
            this.txtDescription.ScrollBars = ScrollBars.Vertical;
            this.txtDescription.Font = new Font("Microsoft YaHei", 9f);

            // 按钮面板
            this.panelButtons = new Panel();
            this.panelButtons.Dock = DockStyle.Bottom;
            this.panelButtons.Height = 50;
            this.panelButtons.BackColor = SystemColors.Control;

            // 保存按钮
            this.btnSave = new Button();
            this.btnSave.Text = "保存";
            this.btnSave.DialogResult = DialogResult.OK;
            this.btnSave.Size = new Size(100, 30);
            this.btnSave.Location = new Point(280, 10);
            this.btnSave.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);
            this.btnSave.Click += (s, e) => SaveBookmark();

            // 取消按钮
            this.btnCancel = new Button();
            this.btnCancel.Text = "取消";
            this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.Size = new Size(100, 30);
            this.btnCancel.Location = new Point(390, 10);
            this.btnCancel.Font = new Font("Microsoft YaHei", 9f);

            // 添加控件到按钮面板
            this.panelButtons.Controls.Add(this.btnSave);
            this.panelButtons.Controls.Add(this.btnCancel);

            // 添加控件到窗体
            this.Controls.Add(this.lblTitle);
            this.Controls.Add(this.txtTitle);
            this.Controls.Add(this.lblUrl);
            this.Controls.Add(this.txtUrl);
            this.Controls.Add(this.btnOpenUrl);
            this.Controls.Add(this.lblCategory);
            this.Controls.Add(this.cmbCategory);
            this.Controls.Add(this.lblDescription);
            this.Controls.Add(this.txtDescription);
            this.Controls.Add(this.panelButtons);

            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void InitializeUI()
        {
            // 初始化分类下拉框
            this.cmbCategory.Items.AddRange(new object[] { "工作", "学习", "娱乐", "个人", "未分类" });
        }

        private void LoadBookmarkData()
        {
            // 加载书签数据到控件
            this.txtTitle.Text = CurrentBookmark.Title;
            this.txtUrl.Text = CurrentBookmark.Url;
            this.txtDescription.Text = CurrentBookmark.Description;

            if (!string.IsNullOrEmpty(CurrentBookmark.Category?.Name))
            {
                this.cmbCategory.SelectedItem = CurrentBookmark.Category.Name;
            }
            else
            {
                this.cmbCategory.SelectedIndex = 0;
            }
        }

        private void SaveBookmark()
        {
            // 验证输入
            if (string.IsNullOrWhiteSpace(this.txtTitle.Text))
            {
                MessageBox.Show("请输入书签标题", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtTitle.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(this.txtUrl.Text) ||
                !Uri.TryCreate(this.txtUrl.Text, UriKind.Absolute, out _))
            {
                MessageBox.Show("请输入有效的URL地址", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtUrl.Focus();
                return;
            }

            // 保存数据到CurrentBookmark
            CurrentBookmark.Title = this.txtTitle.Text.Trim();
            CurrentBookmark.Url = this.txtUrl.Text.Trim();
            CurrentBookmark.Description = this.txtDescription.Text.Trim();
            CurrentBookmark.Category = new Category { Name = this.cmbCategory.SelectedItem.ToString() };
        }

        private void OpenUrl()
        {
            if (!string.IsNullOrWhiteSpace(this.txtUrl.Text))
            {
                try
                {
                    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
                    {
                        FileName = this.txtUrl.Text,
                        UseShellExecute = true
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"无法打开URL: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public static DialogResult ShowEditor(IWin32Window owner, Bookmark bookmark = null)
        {
            using (var dialog = new BookmarkEditor(bookmark))
            {
                return dialog.ShowDialog(owner);
            }
        }
    }
}